# -*- coding: utf-8 -*-

from django import forms
from otica.models import Cliente, Receita
from otica.utils import isUnique

class ClienteForm(forms.ModelForm):
    
    def __init__(self, *args, **kwargs):
        """Preenche o código automaticamente"""
        
        super(ClienteForm, self).__init__(*args, **kwargs)
        self.fields['nome'].widget = forms.TextInput(attrs={'size':60})
        self.fields['endereco'].widget = forms.TextInput(attrs={'size':73})
        self.fields['email'].widget = forms.TextInput(attrs={'size':73})
        self.fields['enderecoTrabalho'].widget = forms.TextInput(attrs={'size':73})
        #if not self.fields['codigo'].initial:
        #    max_codigo = Cliente.gql("ORDER BY codigo DESC LIMIT 1").get()
        #    if max_codigo:
        #        self.fields['codigo'].initial = max_codigo.codigo + 1


    def clean_codigo(self):
        """Não deixa cadatrar o mesmo código duas vezes"""
        
        if 'codigo' in self.cleaned_data:
            value = self.cleaned_data['codigo']
            if value:
                return isUnique(self, 'codigo', value)
        raise forms.ValidationError('Name is required')

    class Meta:
        model = Cliente

class ReceitaForm(forms.ModelForm):
    
    def __init__(self, *args, **kwargs):
        
        super(ReceitaForm, self).__init__(*args, **kwargs)
        self.fields['odEsfericoLonge'].widget = forms.TextInput(attrs={'size':5})
        self.fields['odCilindricoLonge'].widget = forms.TextInput(attrs={'size':5})
        self.fields['odEixoLonge'].widget = forms.TextInput(attrs={'size':5})
        self.fields['odDnpLonge'].widget = forms.TextInput(attrs={'size':5})
        self.fields['odPrismaLonge'].widget = forms.TextInput(attrs={'size':5})
        self.fields['odAlturaLonge'].widget = forms.TextInput(attrs={'size':5})
        self.fields['oeEsfericoLonge'].widget = forms.TextInput(attrs={'size':5})
        self.fields['oeCilindricoLonge'].widget = forms.TextInput(attrs={'size':5})
        self.fields['oeEixoLonge'].widget = forms.TextInput(attrs={'size':5})
        self.fields['oeDnpLonge'].widget = forms.TextInput(attrs={'size':5})
        self.fields['oePrismaLonge'].widget = forms.TextInput(attrs={'size':5})
        self.fields['oeAlturaLonge'].widget = forms.TextInput(attrs={'size':5})
        self.fields['odEsfericoPerto'].widget = forms.TextInput(attrs={'size':5})
        self.fields['odCilindricoPerto'].widget = forms.TextInput(attrs={'size':5})
        self.fields['odEixoPerto'].widget = forms.TextInput(attrs={'size':5})
        self.fields['odDnpPerto'].widget = forms.TextInput(attrs={'size':5})
        self.fields['odPrismaPerto'].widget = forms.TextInput(attrs={'size':5})
        self.fields['oeEsfericoPerto'].widget = forms.TextInput(attrs={'size':5})
        self.fields['oeCilindricoPerto'].widget = forms.TextInput(attrs={'size':5})
        self.fields['odEsfericoLonge'].widget = forms.TextInput(attrs={'size':5})
        self.fields['oeEixoPerto'].widget = forms.TextInput(attrs={'size':5})
        self.fields['oeDnpPerto'].widget = forms.TextInput(attrs={'size':5})
        self.fields['oePrismaPerto'].widget = forms.TextInput(attrs={'size':5})

    class Meta:
        model = Receita
